import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/db/service/LocalConversationService.dart';
import 'package:easy_chat_app/commons/model/Conversation.dart';
import 'package:easy_chat_app/commons/model/GetContactsUserInfoResultData.dart';
import 'package:easy_chat_app/commons/utils/HttpUtils.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/widget/ConfirmDialog.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileContactsInfoSettingPageParams {
  GetContactsUserInfoResultData contactsUserInfo;

  MobileContactsInfoSettingPageParams.create({required this.contactsUserInfo});
}

class MobileContactsInfoSettingPageChangeNotifier extends ChangeNotifier {}

class MobileContactsInfoSettingPage extends StatelessWidget {
  MobileContactsInfoSettingPage({super.key});

  final Logger logger = Logger();

  @override
  Widget build(BuildContext context) {
    var pageParams = RouteUtils.currentRouteSettings().arguments
        as MobileContactsInfoSettingPageParams;
    GetContactsUserInfoResultData contactsUserInfo =
        pageParams.contactsUserInfo;
    return Consumer<AppUserDetailChangeNotifier>(
        builder: (context, appUserDetailChangeNotifier, child) {
      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            return MobileContactsInfoSettingPageChangeNotifier();
          })
        ],
        builder: (context, child) {
          return Scaffold(
            appBar: AppBar(
              title: const Text("资料设置"),
              centerTitle: true,
              surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
              backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
            ),
            body: Container(
              color: const Color.fromRGBO(237, 237, 237, 1),
              child: Column(
                children: [
                  genContactsInfoSettingItem("设置备注", showBottomBorder: false),
                  const SizedBox(
                    height: 8,
                  ),
                  GestureDetector(
                    child: Container(
                      height: 55,
                      alignment: Alignment.center,
                      color: Colors.white,
                      child: const Text(
                        "删除联系人",
                        style: TextStyle(color: Colors.red, fontSize: 18),
                      ),
                    ),
                    onTap: () async {
                      await _deleteContacts(context, contactsUserInfo);
                    },
                  )
                ],
              ),
            ),
          );
        },
      );
    });
  }

  Widget genContactsInfoSettingItem(String itemName,
      {bool showBottomBorder = true,
      Widget? itemWidget,
      void Function()? onTap}) {
    itemWidget ??= Container();
    return GestureDetector(
      onTap: onTap,
      child: Container(
        color: Colors.white,
        width: double.infinity,
        height: 55,
        child: Container(
          margin: const EdgeInsets.only(left: 16, right: 16),
          decoration: BoxDecoration(
              border: showBottomBorder
                  ? const Border(
                      bottom:
                          BorderSide(color: Color.fromRGBO(237, 237, 237, 1)))
                  : null),
          child: Row(
            children: [
              Container(
                height: 54,
                alignment: Alignment.centerLeft,
                child: Text(
                  itemName,
                  style: const TextStyle(fontSize: 16, color: Colors.black),
                ),
              ),
              const Spacer(),
              itemWidget,
              const SizedBox(
                height: 54,
                width: 10,
              ),
              const Icon(Icons.chevron_right_sharp)
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _deleteContacts(BuildContext context,
      GetContactsUserInfoResultData contactsUserInfo) async {
    bool result =
        await ConfirmDialog.show(context, content: const Text("确定要删除此联系人么?"));
    if (result) {
      logger.i("删除好友:$contactsUserInfo");
      Conversation conversation =
          (await HttpUtils.getConversation(contactsUserInfo.id, null)).data!;
      LocalConversationService.deleteChatHistoryByConversationID(
          conversation.id!);
      await HttpUtils.deleteContacts(contactsUserInfo.id!);
      RouteUtils.route2MobileHomePageAndCleanStack();
    }
  }
}
